package com.llh.demo4shiro.controller;

import com.llh.demo4shiro.model.Authority;
import com.llh.demo4shiro.model.Role;
import com.llh.demo4shiro.service.RoleService;
import com.llh.demo4shiro.vo.JsonResult;
import com.llh.demo4shiro.vo.RoleAuth;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>CreatedAt: 2020/9/16 17:27</p>
 *
 * @author llh
 */
@RestController
@RequestMapping("role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    @GetMapping("all")
    public JsonResult all() {
        JsonResult result = new JsonResult();
        List<Role> all = roleService.all();
        result.setData(all);
        return result;
    }

    @PostMapping("add")
    public JsonResult add(@RequestBody Role role) {
        JsonResult result = new JsonResult();
        boolean existed = roleService.existedRoleName(role.getRoleName());
        if (existed) {
            result.setOk(false)
                    .setMsg("角色名已存在");
            return result;
        }
        Role add = roleService.add(role);
        result.setOk(add.getId() > 0);
        return result.setData(add);
    }

    @PostMapping("add/auth")
    public JsonResult addAuth(@RequestBody RoleAuth roleAuth) {
        JsonResult result = new JsonResult();
        int add = roleService.addAuth(roleAuth);
        return result.setData(add);
    }

    @GetMapping("get/auth/{id}")
    public JsonResult getAuth(@PathVariable Integer id) {
        JsonResult result = new JsonResult();
        List<Authority> add = roleService.getAuth(id);
        return result.setData(add);
    }
}


